<?php

class Usuario { 
	var $last_error;
	
	function __construct() {
		$this->last_error="";
	}

  	
	function validate($email,$password) {
		$email = strtolower($email);
		if(!get_magic_quotes_gpc()) $email = addslashes(trim($email));		
		$password=trim($password);
		if(empty($password)) {
			$this->last_error = "Debe escribir un password";
			 return false;
		}
		$email = trim(substr($email,0,50));
		$query = "SELECT password FROM personas WHERE email='$email' AND inactivo<>'1'";
		$result = mysql_query($query);	   		
		if(!$result || (mysql_num_rows($result) < 0)) {
			$this->last_error = "El usuario o password es inv&aacute;lido o no se encuentra activado";
			return false;
		} 
		$fila = mysql_fetch_assoc($result);		
		if(md5($password) != $fila['password']) {
			$this->last_error = "Usuario / Password inv&aacute;lidos";	
			return false;
		};
		return true;
	}
	
	function login($email) {
		$email = mysql_real_escape_string(strtolower($email));
		$query = "SELECT * FROM personas WHERE email = '$email'";
		$result = mysql_query($query);
		if(!$result || mysql_num_rows($result)<1) return false;
		$fila = mysql_fetch_assoc($result);
		session_set("logged","user");	
		session_set("usuario",$fila);
		registrar_log("Ha iniciado Sesion en el sistema");
	}
	 
	
}

class Alumno { 
	var $last_error;
	
	function __construct() {
		$this->last_error="";
	}

  	
	function validate($email,$password) {
		$email = strtolower($email);
		if(!get_magic_quotes_gpc()) $email = addslashes(trim($email));		
		$password=trim($password);
		if(empty($password)) {
			$this->last_error = "Debe escribir un password";
			 return false;
		}
		$email = trim(substr($email,0,50));
		$query = "SELECT password FROM personas WHERE email='$email' AND inactivo<>'1' AND es_dondeaprendo = '1'";
		$result = mysql_query($query);	   		
		if(!$result || (mysql_num_rows($result) < 0)) {
			$this->last_error = "El usuario o password es inv&aacute;lido o no se encuentra activado";
			return false;
		} 
		$fila = mysql_fetch_assoc($result);		
		if(md5($password) != $fila['password']) {
			$this->last_error = "Usuario / Password inv&aacute;lidos";	
			return false;
		};
		return true;
	}
	
	function login($email) {
		$email = mysql_real_escape_string(strtolower($email));
		$query = "SELECT * FROM personas WHERE email = '$email'";
		$result = mysql_query($query);
		if(!$result || mysql_num_rows($result)<1) return false;
		$fila = mysql_fetch_assoc($result);
		session_set("logged","alumno");	
		session_set("usuario",$fila);
		registrar_log("Ha iniciado Sesion en el sistema");
	}
	 
	
}


class Administrador { 
	var $last_error;
	
	function __construct() {
		$this->last_error="";
	}

    function validate($login,$password) {
		$login = strtolower($login);
		if(!get_magic_quotes_gpc()) $login = addslashes(trim($login));		
		$password=trim($password);
		if(empty($password)) {
			$this->last_error = "Debe escribir un password";
			 return false;
		}
		$login = trim(substr($login,0,50));
		$query = "SELECT password FROM administradores WHERE nombre_administrador='$login'";
		$result = mysql_query($query);	   		
		if(!$result || (mysql_num_rows($result) < 0)) {
			$this->last_error = "El usuario o password es inv&aacute;lido o no se encuentra activado";
			return false;
		} 
		$fila = mysql_fetch_assoc($result);		
		if(md5($password) != $fila['password']) {
			$this->last_error = "Usuario / Password inv&aacute;lidos";	
			return false;
		};
		return true;
	}
	
	function login($user) {
		$user = mysql_real_escape_string(strtolower($user));
		$query = "SELECT * FROM administradores WHERE nombre_administrador = '$user'";
		$result = mysql_query($query);
		if(!$result || mysql_num_rows($result)<1) return false;
		$fila = mysql_fetch_assoc($result);
		session_set("logged","admin");	
		session_set("usuario",$user);
		registrar_log("Ha iniciado Sesion");
	}	
}


class Profesor { 
	var $last_error;
	
	function __construct() {
		$this->last_error="";
	}

  	function validate($email,$password) {
		$email = strtolower($email);
		if(!get_magic_quotes_gpc()) $email = addslashes(trim($email));		
		$password=trim($password);
		if(empty($password)) {
			$this->last_error = "Debe escribir un password";
			 return false;
		}
		$email = trim(substr($email,0,50));
		$query = "SELECT password FROM personas WHERE email='$email' AND inactivo<>'1' AND es_profesor = '1' AND es_dondeaprendo = '1'";
		$result = mysql_query($query);	   		
		if(!$result || (mysql_num_rows($result) < 0)) {
			$this->last_error = "El usuario o password es inv&aacute;lido o no se encuentra activado";
			return false;
		} 
		$fila = mysql_fetch_assoc($result);		
		if(md5($password) != $fila['password']) {
			$this->last_error = "Usuario / Password inv&aacute;lidos";	
			return false;
		};
		return true;
	}
	
	function login($email) {
		$email = mysql_real_escape_string(strtolower($email));
		$query = "SELECT * FROM personas WHERE email = '$email'";
		$result = mysql_query($query);
		if(!$result || mysql_num_rows($result)<1) return false;
		$fila = mysql_fetch_assoc($result);
		session_set("logged","profesor");	
		session_set("usuario",$fila);
		registrar_log("Ha iniciado Sesion en el sistema");
	}
}


?>